/**
 * Project Wonderland
 *
 * Copyright (c) 2004-2010, Sun Microsystems, Inc., All Rights Reserved
 *
 * Redistributions in source code form must reproduce the above copyright and
 * this condition.
 *
 * The contents of this file are subject to the GNU General Public License,
 * Version 2 (the "License"); you may not use this file except in compliance
 * with the License. A copy of the License is available at
 * http://www.opensource.org/licenses/gpl-license.php.
 *
 * Sun designates this particular file as subject to the "Classpath" exception
 * as provided by Sun in the License file that accompanied this code.
 */
package org.jdesktop.wonderland.modules.season.server;

import java.util.Observable;
import java.util.Observer;
import org.jdesktop.wonderland.common.cell.ClientCapabilities;
import org.jdesktop.wonderland.common.cell.state.CellComponentClientState;
import org.jdesktop.wonderland.common.cell.state.CellComponentServerState;
import org.jdesktop.wonderland.modules.season.common.SeasonCellComponentClientState;
import org.jdesktop.wonderland.modules.season.common.SeasonCellComponentServerState;
import org.jdesktop.wonderland.modules.season.common.SeasonCellEnum;
import org.jdesktop.wonderland.server.cell.CellComponentMO;
import org.jdesktop.wonderland.server.cell.CellMO;
import org.jdesktop.wonderland.server.comms.WonderlandClientID;

/**
 * The server-side Tooltip Cell Component.
 *
 * @author christian
 */
public class SeasonCellComponentMO extends CellComponentMO {

    private static SeasonCellEnum season;
    private static SeasonObservable notifier;

    /**
     * @return the season
     */
    public static SeasonCellEnum getSeason() {
        return season;
    }

    /**
     * @param aSeason the season to set
     */
    public static void setSeason(SeasonCellEnum aSeason) {
        season = aSeason;
    }

    /**
     * @return the notifier
     */
    public static SeasonObservable getNotifier() {
        return notifier;
    }

    /**
     * @param aNotifier the notifier to set
     */
    public static void setNotifier(SeasonObservable aNotifier) {
        notifier = aNotifier;
    }

    /**
     * Constructor, takes the CellMO associated with the Cell Component.
     *
     * @param cell The CellMO associated with this component
     */
    public SeasonCellComponentMO(CellMO cell) {
        super(cell);

        if (notifier == null) {
            notifier = new SeasonObservable();
        }

        try {
            Observer ob = (Observer) cell;
            notifier.addCellObserver(ob);
        } catch (Exception ex) {
            System.out.println("SERVRE SEASON ERROR: Cast to Observer not possible");
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected String getClientClass() {
        return "org.jdesktop.wonderland.modules.season.client"
                + ".SeasonCellComponent";
    }

    public void addObserver(Observer ob) {
        getNotifier().addCellObserver(ob);
        System.out.println("SERVER SEASON Notifer added");
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public CellComponentClientState getClientState(
            CellComponentClientState state, WonderlandClientID clientID,
            ClientCapabilities capabilities) {

        if (state == null) {
            state = new SeasonCellComponentClientState();
        }
        ((SeasonCellComponentClientState) state).setSeason(this.getSeason());
        return super.getClientState(state, clientID, capabilities);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public CellComponentServerState getServerState(
            CellComponentServerState state) {

        if (state == null) {
            state = new SeasonCellComponentServerState();
        }
        ((SeasonCellComponentServerState) state).setSeason(this.getSeason());
        return super.getServerState(state);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void setServerState(CellComponentServerState state) {
        super.setServerState(state);
        this.setSeason(((SeasonCellComponentServerState) state).getSeason());
        notifier.notifyCells(season.toString());
    }
    
    
    
    private class SeasonObservable extends Observable {

        public void addCellObserver(Observer ob) {
            this.addObserver(ob);
            System.out.println("SERVER SEASON Observer added: " + ob.toString());
        }

        public void notifyCells(String newSeason) {
            System.out.println("SERVER SEASON Notifyed with Season " + newSeason);
            this.setChanged();
            notifyObservers(newSeason);
        }
    }
}
